// This file is part of OpenTSDB.
// Copyright (C) 2017 TeleDB Development Group.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version.  This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
// General Public License for more details.  You should have received a copy
// of the GNU Lesser General Public License along with this program.  If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.tsd.service;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import net.opentsdb.core.TSDB;
import net.opentsdb.tsd.HttpQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 删除时间线请求
 */
public class DeleteLocalTsuidService extends RpcServiceImpl{

    private static final Logger LOG = LoggerFactory.getLogger(DeleteLocalTsuidService.class);

    public DeleteLocalTsuidService(TSDB tsdb, HttpQuery query){ super(tsdb, query); }

    @Override
    public void doGet() {
        doDelete();
    }

    @Override
    public void doPost() {
        doDelete();
    }

    @Override
    public void doDelete() {

        String tsuid = String.valueOf(((Map<String,String>)params).get("tsuid"));
        LOG.debug("delete tsuid: "+tsuid);

        if(Strings.isNullOrEmpty(tsuid)){
            LOG.error("tsuid {} is null，params context：{} ", tsuid,
                    Joiner.on(" ").join(((Map<String, String>) params).entrySet()));
            putErrorResponse(false, "delete tsuid: "+tsuid+" error");
            return;
        }

        try{
            tsdb.deleteMapTsuid(tsuid, true);//deleteHbaseTSMeta(tsuid);
            putResponse(true,"delete tsuid success");
        } catch (Exception e) {
            LOG.debug("delete tsuid: "+tsuid+" error:",e.getMessage());
            putErrorResponse(false, "delete tsuid: "+tsuid+" error");
        }
    }

    @Override
    public void checks() {
        Map<String,String> map = (Map<String,String>)params;

        if(!map.containsKey("tsuid")){
            LOG.info("error delete tsuid request");
            putErrorResponse(false,"error delete type request, ex:{tsuid:'10'}");
        }
    }
}
